﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// PaperHandler 的摘要说明
    /// </summary>
    public class PaperHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.Params["type"];
            if (string.IsNullOrEmpty(type))
                type = "getlist";

            var ID = context.Request.QueryString["ID"];

            OnLineExam.BLL.PaperManager bll = new OnLineExam.BLL.PaperManager();

            switch (type)
            {
                case "getlist":
                    #region 获取列表

                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);

                    string depid = context.Request.Params["depid"];
                    int startIndex = (page - 1) * rows + 1;
                    int endIndex = (page) * rows;

                    //var loginuser=new UserManager().GetModel(context.User.Identity.Name);
                    var ds = bll.GetListByPage("depid=" + depid, "PaperID", startIndex, endIndex);
                    int RecordCount = bll.GetRecordCount("depid=" + depid);
                    context.Response.Write(HHG.Common.Json.DataTable2Json(ds.Tables[0], RecordCount));
                    #endregion
                    break;
                case "getlistForSelect":
                    #region 获取下拉列表
                    depid = context.Request.Params["depid"];
                    ds = bll.GetList("depid='"+depid+"'");
                    context.Response.Write(HHG.Common.Json.DataTable2Json(ds.Tables[0], false));
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息

                    if (!string.IsNullOrEmpty(ID))
                    {
                        var m = bll.GetModel(int.Parse(ID));
                        //m.ExamTime = DateTime.Now;
                        JavaScriptSerializer json = new JavaScriptSerializer();
                        var rsult = json.Serialize(m);
                        rsult = Regex.Replace(rsult, @"\\/Date\((\d+)\)\\/", match =>
                        {
                            DateTime dt = new DateTime(1970, 1, 1);
                            dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                            dt = dt.ToLocalTime();
                            return dt.ToString("yyyy-MM-dd");
                        });
                        //var rsult = HHG.Common.Json.GetJson<OnLineExam.Model.Paper>(m);
                        context.Response.Write(rsult);
                    }

                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                    depid = context.Request.QueryString["depid"];
                     
                    var new_model = new OnLineExam.Model.Paper();
                    
                    new_model.DepID = int.Parse(depid);
                    new_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    new_model.PaperName = context.Request.Params["txtPaperName"];
                    new_model.startExamTime = DateTime.Parse(context.Request.Params["txtStartDateTime"]);
                    new_model.EndExamTime = DateTime.Parse(context.Request.Params["txtEndDateTime"]);
                    new_model.ExamTime = context.Request.Params["txtExamTime"];
                    new_model.allowExamNum =int.Parse( context.Request.Params["txtallowExamNum"]);
                    new_model.passMark = int.Parse(context.Request.Params["txtpassmark"]);
                    new_model.joinemps = context.Request.Params["txtJoinEmpID"];
                    new_model.ProblemInfo = "单选:{0}:{1},多选:{2}:{3},判断:{4}:{5},填空:{6}:{7},问答:{8}:{9}";
                    new_model.ProblemInfo=string.Format(new_model.ProblemInfo,
                        context.Request.Params["txtSingleNum"], context.Request.Params["txtSingleFen"],
                        context.Request.Params["txtMultiNum"], context.Request.Params["txtMultiFen"],
                        context.Request.Params["txtJudgeNum"], context.Request.Params["txtJudgeFen"],
                        context.Request.Params["txtFillBlankNum"], context.Request.Params["txtFillBlankFen"],
                        context.Request.Params["txtQuestionNum"], context.Request.Params["txtQuestionFen"]
                        );

                    //new_model.ProblemInfo = "0";
                    int nID = bll.Add(new_model);
                    context.Response.Write(nID);//添加成功，返回ID,失败，返回0

                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    ID = context.Request.Params["hdID"];
                    var o_model = bll.GetModel(int.Parse(ID));
                    //new_model = new OnLineExam.Model.JudgeProblem();
                    o_model.PaperID = int.Parse(ID);
                    o_model.DepID = o_model.DepID;
                    o_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    o_model.PaperName = context.Request.Params["txtPaperName"];
                    o_model.startExamTime = DateTime.Parse(context.Request.Params["txtStartDateTime"]);
                    o_model.EndExamTime = DateTime.Parse(context.Request.Params["txtEndDateTime"]);
                    o_model.ExamTime = context.Request.Params["txtExamTime"];
                    o_model.allowExamNum = int.Parse(context.Request.Params["txtallowExamNum"]);
                    o_model.passMark = int.Parse(context.Request.Params["txtpassmark"]);
                    o_model.joinemps = context.Request.Params["txtJoinEmpID"];
                    o_model.ProblemInfo = "单选:{0}:{1},多选:{2}:{3},判断:{4}:{5},填空:{6}:{7},问答:{8}:{9}";
                    o_model.ProblemInfo = string.Format(o_model.ProblemInfo,
                        context.Request.Params["txtSingleNum"], context.Request.Params["txtSingleFen"],
                        context.Request.Params["txtMultiNum"], context.Request.Params["txtMultiFen"],
                        context.Request.Params["txtJudgeNum"], context.Request.Params["txtJudgeFen"],
                        context.Request.Params["txtFillBlankNum"], context.Request.Params["txtFillBlankFen"],
                        context.Request.Params["txtQuestionNum"], context.Request.Params["txtQuestionFen"]
                        );

                    if (bll.Update(o_model))
                    {
                        context.Response.Write(ID);//修改成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败，返回0
                    }
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"];
                    if (bll.Delete(int.Parse(ID)))
                    {
                        context.Response.Write(ID);//删除成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//删除失败，返回0
                    }
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}